use std::cell::RefCell;
use std::rc::Rc;
use crate::instruction::instruction::Instruction;
use crate::vm_thread::{VMFrame, VMThread};

pub struct goto {
    pub offset: u16,
}

impl Instruction for goto {
    fn read_operands(&mut self, frame: Rc<RefCell<VMFrame>>) {
        self.offset = frame.borrow_mut().read_u16();
    }

    fn exec(&mut self, frame: Rc<RefCell<VMFrame>>, thread: &VMThread) {
        let mut rw_frame = frame.borrow_mut();
        rw_frame.pc = rw_frame.pc - 3 + (self.offset as usize);
    }

    fn to_string(&self) -> String {
        String::from("goto")
    }
}